package com.fulihui.wgj.datastatis.dal.sharding;

import io.shardingsphere.core.api.algorithm.sharding.PreciseShardingValue;
import io.shardingsphere.core.api.algorithm.sharding.standard.PreciseShardingAlgorithm;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collection;

/**
 * sharding-jdbc 3.0 table algorithm
 *
 * @author: Levon
 * @version: v 0.1 2017-10-25 10:10
 */
public class TBArticleTotalStatDetailAlgorithm implements PreciseShardingAlgorithm<String> {

    protected final transient Logger LOGGER = LoggerFactory.getLogger(getClass());
    private final int TABLE_COUNT = 20;
    private final String DEFAULT_TABLE = "wx_article_total_stat_detail";

    @Override
    public String doSharding(Collection<String> tablenames, PreciseShardingValue<String> shardingValue) {
        Integer suffix = Math.abs(
                new HashCodeBuilder().append(shardingValue.getValue()).hashCode() % TABLE_COUNT);
        for (String each : tablenames) {
            if (each.endsWith("_" + suffix)) {
                return each;
            }
        }
        return DEFAULT_TABLE;
    }


}
